package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// FlightTask 飞机任务
type FlightTask struct {
	BaseModel
	Code              string     `orm:"column(code);description(编号)" json:"code"`
	PlanStart         *time.Time `orm:"column(plan_start);description(计划开始时间)" json:"planStart"`
	PlanEnd           *time.Time `orm:"column(plan_end);description(计划结束时间)" json:"planEnd"`
	PlanDuration      int        `orm:"column(plan_duration);description(计划时长:秒)" json:"planDuration"`
	ActualDuration    int        `orm:"column(actual_duration);description(实际时长:秒)" json:"actualDuration"`
	PlanUpDownCount   int        `orm:"column(plan_up_down_count);description(计划起落次数)" json:"planUpDownCount"`
	ActualUpDownCount int        `orm:"column(actual_up_down_count);description(实际起落次数)" json:"actualUpDownCount"`
	TaskSceneId       int64      `orm:"column(task_scene_id);description(任务场景字典Id)" json:"taskSceneId"`
	TaskTypeId        int64      `orm:"column(task_type_id);description(任务类型字典Id)" json:"taskTypeId"`
	TargetAirportId   int64      `orm:"column(target_airport_id);description(目标机场Id)" json:"targetAirportId"`
	AircraftId        int64      `orm:"column(aircraft_id);description(飞机Id)" json:"aircraftId"`
	PilotName         string     `orm:"column(pilot_name);description(飞行员名字)" json:"pilotName"`
}

func init() {
	orm.RegisterModel(new(FlightTask))
}

// SearchField 定义模型的可搜索字段
func (*FlightTask) SearchField() []string {
	return []string{"code"}
}

// WhereField 定义模型可作为条件的字段
func (*FlightTask) WhereField() []string {
	return []string{"id", "status", "code"}
}

// SimpleField 简略查询字段
func (*FlightTask) SimpleField() []string {
	return []string{"id", "status", "code"}
}

func (m *FlightTask) TableName() string {
	return "flight_task"
}

// AddFlightTask insert a new FlightTask into database and returns
// last inserted Id on success.
func AddFlightTask(m *FlightTask, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetFlightTaskById retrieves FlightTask by Id. Returns error if
// Id doesn't exist
func GetFlightTaskById(id int64) (v *FlightTask, err error) {
	o := orm.NewOrm()
	v = &FlightTask{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(FlightTask)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateFlightTaskById updates FlightTask by Id and returns error if
// the record to be updated doesn't exist
func UpdateFlightTaskById(m *FlightTask, userId int64) (err error) {
	o := orm.NewOrm()
	v := FlightTask{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteFlightTask deletes FlightTask by Id and returns error if
// the record to be deleted doesn't exist
func DeleteFlightTask(id int64) (err error) {
	o := orm.NewOrm()
	v := FlightTask{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&FlightTask{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyFlightTask Destroy FlightTask by Id and returns error if
func DestroyFlightTask(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := FlightTask{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
